﻿using log4net;
using Sunny.UI;

namespace FZRD

{
    public partial class Page6 : UIPage
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Page6));
        private List<SystemUserEntity> entityList = new List<SystemUserEntity>();

        public Page6()
        {
            InitializeComponent();

            uiDataGridView1.AddColumn("用户ID", "ID").SetFixedMode(100);
            uiDataGridView1.AddColumn("用户名", "UserName").SetFixedMode(100);
            uiDataGridView1.AddColumn("用户分组", "GroupName").SetFixedMode(150);
            uiDataGridView1.AddColumn("操作权限", "Permission").SetFixedMode(150);
            uiDataGridView1.AddColumn("创建时间", "CreateTime").SetFixedMode(150);
            uiDataGridView1.AddColumn("登录时间", "LoginTime").SetFixedMode(150);
            uiDataGridView1.AddColumn("登出时间", "LogoutTime").SetFixedMode(150);
        }
        public override void Init()
        {
            base.Init();

            RefreshTable();

        }
        public void RefreshTable()
        {
            entityList.Clear();

            try
            {
                var sql = @"select u.ID, u.UserName, u.GroupID, g.GroupName, u.Permission, u.CreateTime, u.LoginTime, u.LogoutTime from system_user u JOIN system_user_group g ON u.GroupID = g.ID";
                var dt = MainForm.db.SelectDataTable(sql);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var row = dt.Rows[i];
                    var func = new SystemUserEntity()
                    {
                        ID = (int)row["ID"],
                        UserName = row["UserName"] != DBNull.Value ? row["UserName"].ToString() : "",
                        GroupID = row["GroupID"] != DBNull.Value ? (int)row["GroupID"] : 0,
                        GroupName = row["GroupName"] != DBNull.Value ? row["GroupName"].ToString() : "",
                        Permission = row["Permission"] != DBNull.Value ? (int)row["Permission"] : 0,
                        CreateTime = row["CreateTime"] != DBNull.Value ? (long)row["CreateTime"] : 0,
                        LoginTime = row["LoginTime"] != DBNull.Value ? (long)row["LoginTime"] : 0,
                        LogoutTime = row["LogoutTime"] != DBNull.Value ? (long)row["LogoutTime"] : 0
                    };
                    entityList.Add(func);
                }

                if (entityList.Count > 0)
                {
                    uiPagination1.DataSource = entityList;
                    uiPagination1.ActivePage = 1;
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
        }
        private void uiPagination1_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            uiDataGridView1.DataSource = pagingSource;
        }

        private void btnAdd_Click(object sender, System.EventArgs e)
        {
            FUserAddForm frm = new FUserAddForm();
            frm.ShowDialog();
            if (frm.IsOK)
            {
                try
                {
                    MainForm.db.ExecuteSql(
                        MainForm.db.Insert("system_user")
                            .SetColumn("UserName", frm.UserAddEntity.UserName)
                            .SetColumn("Password", frm.UserAddEntity.Password)
                            .SetColumn("GroupID", frm.UserAddEntity.GroupID)
                            .SetColumn("Permission", frm.UserAddEntity.Permission)
                            .SetColumn("CreateTime", frm.UserAddEntity.CreateTime)
                            .ToSql());
                    RefreshTable();
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
                RecordUserOperation("添加用户 frm.UserAddEntity=" + frm.UserAddEntity.ToString());
            }
            frm.Dispose();
        }

        private void btnEdit_Click(object sender, System.EventArgs e)
        {
            int index = this.uiDataGridView1.SelectedIndex;
            SystemUserEntity systemUserEntity = entityList.Get(index);
            if (systemUserEntity != null)
            {
                UserEditEntity userEditEntity = new UserEditEntity();
                userEditEntity.UserName = systemUserEntity.UserName;
                userEditEntity.Password = systemUserEntity.Password;
                userEditEntity.GroupID = systemUserEntity.GroupID;
                userEditEntity.Permission = systemUserEntity.Permission;
                userEditEntity.CreateTime = systemUserEntity.CreateTime;

                FUserEditForm frm = new FUserEditForm();
                frm.userEditEntity = userEditEntity;
                frm.ShowDialog();
                if (frm.IsOK)
                {
                    try
                    {
                        MainForm.db.ExecuteSql(
                            MainForm.db.Update("system_user")
                                .SetColumn("UserName", frm.userEditEntity.UserName)
                                .SetColumn("Password", frm.userEditEntity.Password)
                                .SetColumn("GroupID", frm.userEditEntity.GroupID)
                                .SetColumn("Permission", frm.userEditEntity.Permission)
                                .SetColumn("CreateTime", frm.userEditEntity.CreateTime)
                                .Where("ID=" + frm.userEditEntity.ID)
                                .ToSql());

                        RefreshTable();
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex.Message);
                    }
                    RecordUserOperation("编辑用户 frm.userEditEntity=" + frm.userEditEntity.ToString());
                }
                frm.Dispose();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int index = this.uiDataGridView1.SelectedIndex;
            SystemUserEntity systemUserEntity = entityList.Get(index);
            if (systemUserEntity != null)
            {
                try
                {
                    MainForm.db.ExecuteSql(
                        MainForm.db.Delete("system_user")
                            .Where("ID=" + systemUserEntity.ID)
                            .ToSql());
                    RefreshTable();
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
                RecordUserOperation("删除用户 frm.systemUserEntity=" + systemUserEntity.ToString());
            }
        }

        private void cbUserGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.cbUserGroup.SelectedIndex;
            List<SystemUserEntity> filterEntityList = new List<SystemUserEntity>();
            if (entityList.Count > 0)
            {
                for (int i = 0; i < entityList.Count; i++)
                {
                    SystemUserEntity entity = entityList[i];
                    if (index == entity.GroupID)
                    {
                        filterEntityList.Add(entity);
                    }
                }
                uiPagination1.DataSource = entityList;
            }
        }

        public void RecordUserOperation(string operation)
        {
            try
            {
                string userName = LoginForm.username;
                string userIp = "127.0.0.1";
                long recordTime = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeMilliseconds(); ;
                MainForm.db.ExecuteSql(
                    MainForm.db.Insert("system_operation_content")
                        .SetColumn("UserName", userName)
                        .SetColumn("UserIP", userIp)
                        .SetColumn("RecordTime", recordTime)
                        .SetColumn("Operation", operation)
                        .ToSql());
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
        }
    }
}